package com.amumu.drama.portal.modules.member.controller;

import com.amumu.drama.common.base.api.CommonResult;
import com.amumu.drama.common.modules.member.service.UmsMemberService;
import com.amumu.drama.model.UmsMember;
import com.amumu.drama.portal.config.JWTProperties;
import com.amumu.drama.portal.modules.member.dto.req.IdCardVerifyReq;
import com.amumu.drama.portal.modules.member.dto.req.LoginByPhoneReq;
import com.amumu.drama.portal.modules.member.dto.req.PhoneSendCodeReq;
import com.amumu.drama.portal.modules.member.dto.resp.MemberInfoResp;
import com.amumu.drama.portal.modules.member.service.MemberService;
import com.amumu.drama.portal.service.AuthService;
import com.amumu.drama.portal.util.MemberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "用户登录")
@RestController
@RequestMapping("/api/v1/auth")
public class AuthController {

    @Autowired
    private JWTProperties jwtProperties;
    @Autowired
    private UmsMemberService umsMemberService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private AuthService authService;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @ApiOperation(value = "微丸子-获取短信验证码")
    @RequestMapping(value = "/captcha/sms", method = RequestMethod.POST)
    public CommonResult<Boolean> sendSmsCaptcha(@RequestBody @Validated PhoneSendCodeReq request) {
        return umsMemberService.sendSmsCaptcha(request.getPhoneNum());
    }

    @ApiOperation(value = "微丸子-短信验证码登录")
    @RequestMapping(value = "/captcha/login", method = RequestMethod.POST)
    public CommonResult<Map<String, String>> loginBySmsCaptcha(@RequestBody @Validated LoginByPhoneReq req) {
        String token = authService.loginBySmsCaptcha(req.getPhoneNum(), req.getCaptcha());
        if (token == null) {
            return CommonResult.failed("用户名或验证码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", jwtProperties.getTokenHead());
        return CommonResult.success(tokenMap);
    }

    @ApiOperation(value = "微丸子-实名认证")
    @PostMapping(value = "/idCard/verify")
    public CommonResult<String> verifyIdCard(@RequestBody @Validated IdCardVerifyReq request) {
        return umsMemberService.verifyIdCard(MemberUtil.getCurrentMemberId(), request.getRealName(), request.getIdCard());
    }

//    @ApiOperation(value = "微丸子-获取微信openId")
//    @GetMapping("/wx/openId")
//    public CommonResult<String> getWXOpenId(UmsMember umsMember, String code) {
//        return memberService.getOpenId(umsMember, code);
//    }

    @ApiOperation(value = "微丸子-刷新token")
    @RequestMapping(value = "/refreshToken", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult refreshToken(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        String refreshToken = authService.refreshToken(token);
        if (refreshToken == null) {
            return CommonResult.failed("token已经过期！");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", refreshToken);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }

    @ApiOperation("微丸子-获取会员信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<MemberInfoResp> info(Principal principal) {
        if (principal == null) {
            return CommonResult.unauthorized(null);
        }
        UmsMember member = authService.getCurrentMember();
        return CommonResult.success(memberService.queryMemberInfo(member));
    }
}
